package com.reyco1.services.social
{
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;

	public class SocialShare
	{
		public function SocialShare()
		{
		}
		
		// FACEBOOK
		public static function shareFB(link:String, title:String):void
		{
			openPage('http://www.facebook.com/sharer/sharer.php?t=' + title.split(" ").join("+") + '&u=' + escape(link),"_popup");
		}
		
		// TWITTER
		public static function shareTwitter(link:String, title:String):void
		{
			openPage('https://twitter.com/intent/tweet?source=webclient&text=' + title.split(" ").join("+") + '%3A+' + escape(link),"_popup");
		}
		
		// MAIL
		public static function shareMail(address:String, subject:String, body:String):void
		{
			var request:URLRequest = new URLRequest("mailto:"+address+"?subject="+subject+"&body="+"\n\n" + body);           
			navigateToURL(request, "_self");
		}
		
		// TUMBLR
		public static function shareTumblr(link:String, name:String, description:String):void
		{
			openPage("http://www.tumblr.com/share/link?url=" + escape(link) + "&name=" + escape(name) + "&description=" + escape(description),'_popup');
		}
		
		// STUMBLE UPON
		public static function shareSU(link:String, title:String):void
		{
			openPage("http://www.stumbleupon.com/submit?url="+escape(link)+"&title="+escape(title));
		}
		
		// GOOGLE +
		public static function shareGoogle(link:String):void
		{
			openPage("https://m.google.com/app/plus/x/?v=compose&content="+escape(link),"_popup");
		}
		
		// DIGG
		public static function shareDigg(link:String, title:String, body:String, topic:String):void
		{
			openPage("http://digg.com/submit?phase=2&url="+escape(link)+"&title="+escape(title)+"&bodytext="+escape(body)+"&topic="+escape(topic));
		}
		
		//REDDIT
		public static function shareReddit(link:String):void
		{
			openPage("http://www.reddit.com/submit?url="+escape(link),'_popup');
		}
		
		// DELICIOUS
		public static function shareDelicious(link:String, title:String):void
		{
			openPage("http://www.delicious.com/save?v=5&jump=close&url="+escape(link)+"&title="+escape(title));
		}
		
		public static function openPage(url:String, linkWindow:String = "_blank", popUpDimensions:Array = null):void 
		{
			if (linkWindow == "_popup" && ExternalInterface.available) 
			{
				var dimensions:Array = [640,480];
				ExternalInterface.call("window.open('" + url + "','PopUpWindow','width=" + dimensions[0] + ",height=" + dimensions[1] + ",toolbar=yes,scrollbars=yes')");
			} 
			else 
			{
				// Use JS to bypass popup blockers if ExternalInterface is available
				var window:String = linkWindow == "_popup" ? "_blank" : linkWindow;
				if (ExternalInterface.available) 
				{
					ExternalInterface.call('window.open("' + url + '","' + window + '")');
				} 
				else 
				{
					//request a blank page
					navigateToURL(new URLRequest(url), window);
				}
			}
		}
	}
}